package cn.ithers.blog.controller.portal;

import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IArticleService;
import cn.ithers.blog.service.ICategoryService;
import cn.ithers.blog.utils.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 16:39
 * @description:
 */
@Api(value = "文章", tags = "文章")
@RestController
@RequestMapping("/portal/article")
public class ArticlePortalApi {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 获取文章列表
     * 权限是所有用户
     * 状态必须是已经发布到的，置顶的由另一个接口获取，其他到的不可以从此接口获取
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取文章列表")
    @GetMapping("/list/{page}/{pageSize}")
    public ResponseResult listArticle(@ApiParam(value = "当前页码", required = true) @PathVariable("page") int page,
                                      @ApiParam(value = "每页显示条数", required = true) @PathVariable("pageSize") int pageSize) {
        return articleService.articleList(page, pageSize, null, null, Constants.Article.STATE_PUBLISH);
    }

    /**
     * 根据分类获取内容category
     *
     * @param categoryId
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("根据分类获取内容")
    @GetMapping("/list/{categoryId}/{page}/{pageSize}")
    public ResponseResult listArticleByCategoryId(@ApiParam(value = "分类ID", required = true) @PathVariable("categoryId") String categoryId,
                                                  @ApiParam(value = "当前页码", required = true) @PathVariable("page") int page,
                                                  @ApiParam(value = "每页显示条数", required = true) @PathVariable("pageSize") int pageSize) {
        return articleService.articleList(page, pageSize, null, categoryId, Constants.Article.STATE_PUBLISH);
    }

    /**
     * 获取文章详情
     * 权限 ：任意用户
     * 内容过滤：只允许拿置顶的，或已经发布的
     * <p>
     * 其他到的需要权限，比如说草稿，只能对应用户货啊u
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("获取文章详情")
    @GetMapping("/{categoryId}")
    public ResponseResult getArticleDetail(@ApiParam(value = "分类ID", required = true) @PathVariable("categoryId") String categoryId) {
        return articleService.getArticleById(categoryId);
    }

    /**
     * 获取推荐文章
     * 通过标签来计算这个匹配度
     * 标签：由一个或多个（5个以内，包含5个）
     * 从里面随机拿一个标签-》每次获取的推荐文章，不那么雷同
     * 通过标签查询类似的文章，所包含此标签的文章,如果没有相关文章，就从数据中获取最新的文章
     *
     * @param articleId
     * @return
     */
    @ApiOperation("获取推荐文章")
    @GetMapping("/recommend/{articleId}/{size}")
    public ResponseResult getRecommendArticle(@ApiParam(value = "分类ID", required = true) @PathVariable("articleId") String articleId,
                                              @ApiParam(value = "设置推荐文章数量", required = true) @PathVariable("size") int size) {
        return articleService.listRecommendArticle(articleId, size);
    }

    /**
     * 获取标签列表
     * 用户点击标签，就会获取相关文章列表
     *
     * @param size
     * @return
     */
    @ApiOperation("获取标签列表")
    @GetMapping("/label/{size}")
    public ResponseResult getLabels(@ApiParam(value = "获取标签数量", required = true) @PathVariable("size") int size) {
        return articleService.listLabels(size);
    }

    /**
     * 通过标签获取文章列表
     *
     * @param label
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("通过标签获取文章列表")
    @GetMapping("/list/label/{label}/{page}/{pageSize}")
    public ResponseResult listArticleByLabel(@ApiParam(value = "标签", required = true) @PathVariable("label") String label,
                                             @ApiParam(value = "当前页码", required = true) @PathVariable("page") int page,
                                             @ApiParam(value = "每页显示条数", required = true) @PathVariable("pageSize") int pageSize) {
        return articleService.articleListByLabel(page, pageSize, label);
    }

    /**
     * 获取置顶文章列表
     *
     * @return
     */
    @ApiOperation("获取置顶文章列表")
    @GetMapping("/top")
    public ResponseResult getTopArticle() {
        return articleService.listTopArticle();
    }

    /**
     * 获取分类categories
     *
     * @return
     */
    @ApiOperation("获取分类")
    @GetMapping("/categories")
    public ResponseResult getCategories() {
        return categoryService.getCategoryList();
    }
}
